var mind = {
    /*
    * 创建元素
    * @param: name - 元素名
    * @param: args {Object}
    *       {
    *           attrs: [{ name: 'class', value:'xxx'}],
    *           childs: [Element],
    *           innerHtml: '字符串'
    *       }
    * 返回值: Element
    */
    createElement:function (name, args) {
        var element = document.createElement(name);
        var attrs = args?args.attrs:[];
        if(attrs&&attrs.length>0){
            for(var i=0; i<attrs.length; i++) {
                if(attrs[i].value){
                    element.setAttribute(attrs[i].name, attrs[i].value);
                } else {
                    element.setAttribute(attrs[i].name, '');
                }
            }
        }
        var childs = args?args.childs:[];
        if(childs&&childs.length>0){
            for (var j = 0; j < childs.length; j++) {
                element.appendChild(childs[j]);
            };
        }
        if(args&&args.innerHTML){
            element.innerHTML = args.innerHTML;
        }
        return element;
    },
    alert: function(message, type, title)
    {
        if(type=='success')
            toastr.success(message, title||false);
        else if(type=='warning')
            toastr.warning(message, title||false);
        else if(type=='danger'||type==='error')
            toastr.error(message, title||false);
        else{
            toastr.info(message, title||false);
        }
    },
    validateInfo: function(obj,text,type)
    {
        var alertClass='has-error';
        switch(type)
        {
            case 'error':
                alertClass='has-error';
            break;
            case 'success':
                alertClass='has-success';
            break;
            case 'warning':
                alertClass='has-warning';
            break;

        };
        $(obj).parent('div').addClass(alertClass);
        var span = '<span class="help-block has-error">'+text+'</span>';
        $(obj).parent('div').after(span);
    },
    ajaxOrder: function(url) {
        $.ajax({
            url: url,
            type:'post',
            success: function (result) {
                if(result.code==0){
                    window.location.reload();
                } else {
                    $('div .main-content').prepend(mind.alert('danger', result.message, false));
                }
            }
        })
    }
}

$(function(){
    $('form :input').click(function(){
        $(this).parents('div').removeClass('has-error');
        $(this).parents('div').next('span.help-block').text('');
    })
    $('.form-group').click(function(){
        $(this).removeClass('has-error');
        $(this).next('span').remove();
    })
})

